TRU Board Games Playground - Java

  1. Introduction
    • An example, creation and solving a maze - .
    • The board of maze is created by using this library. A path between the entrance and exit is found and displyed with Green color.
  2. Download this for the following Java programs with their class files under 'trubgp', and compile them if necessary. They are used in TRU Board Games Playground as the package named 'trubgp'.
  3. 3-puzzle game
    • Download BGP_nPuzzle.java, fix a bit, compile it with the above library, and play the game.
      Can you revise the program so that 4 x 4 board game is supported?
  4. API
    • Each cell has
      • position - integers for row and col from 0
      • background color - Color; E.g., Color.LIGHT_GRAY, Color.RED, Color.WHITE, Color.CYAN, ...
      • content - String
    • Board styles:
      • "Line"
      • "noLine"
    • Board(int rows, int columns, int width, int height, String style, Color color) - object constructor
      • rows × columns board, i.e., cells
      • height of the board - integer for px
      • width of the board - integer for px
      • board_style - string
      • cell background color - E.g., Color.LIGHT_GRAY, Color.RED, Color.WHITE, ...
      • Example of BGP_nPuzzle.java - private static Board board = new Board(3, 3, 500, 500, "Line", Color.LIGHT_GRAY); shown in the above 3.
      • Note. import.awt.*; is required.
    • Methods in the Board object
      • void setTitle(String title) - E.g., board.setTitle("3-Puzzle Game");
      • void cellsClickEventListener(BGPEventListener listener) - register a 'click' event handler on all cells
        board.cellsClickEventListener(new BGPEventListener() { @Override public void clicked(int row, int col) { cellClicked(row, col); // A method in the user program }}); // BGPEventListener in the trubgp library
      • Color cellBackgroundColor(int row, int col) - return the background color of the cell pointed by row and col
      • void cellBackgroundColor(int row, int col, Color color) - set the background color of the cell
      • String cellContent(int row, int col) - return the cell content
      • void cellContent(int row, int col, String content) - set cell content with the default font size
      • void cellContent(int row, int col, String content, float font_size) - set cell content with a specific font size
      • void switchCells(int row0, int col0, int row1, int col1) - switch all data stored/set in the two cells
      • void button1ClickEventListener(BGPEventListener listener) - register an click event handler on the 1st button
      • void button2ClickEventListener(BGPEventListener listener) - register an click event handler on the 2nd button
      • void button1SetName(String name)
      • void button2SetName(String name)
      • void showMessageDialog(String message) - display a dialog popup window
      • String getText() - read the string from the text field
      • void setText(String s) - write a string in the text field
      • void clearText() - clear the text area
      • void appendText(String s) - append a string at the end of the string in the text area
  5. Trial 1: Let's try to create a game board and display it. When you click a cell on the board, the color will be changed. You can save the code in a Java file and test it.